/*******************************************************
*  Team name: FTPMP
*  Class involve: RoundWindow, BoardEngine, DiceGenerator, CardWindow
*  Version, Author, History of modification: 
*  Version 1.1, Fred,
*  	- add JPanel, JLabel
*  Version 1.2, Fred,
*   - add setMTPanel, setActionPanel, addUseMTPanel, dice, diceValue
*  Version 1.3
*  	- add DrawMTPanel
*******************************************************/
package boundary;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.border.LineBorder;

import javax.swing.JLabel;

import control.BoardEngine;
import control.DiceGenerator;
import java.awt.Font;

import javax.swing.JProgressBar;

public class RoundWindow extends JPanel implements ActionListener
{
	
	private static final long serialVersionUID = 1019516868181138670L;
	int[] playerPos;
	int playerNo;
	Icon[] playerIcons;
	int currentPlayer;
	boolean btnState;
	BoardEngine boardEngine;
	//var for GUI
	CardLayout cl;
	//left-most panel for SE status
	JPanel StatusPanel;
	JLabel[] SP_Player;
	JLabel totalSElbl=new JLabel("Total SE");
	//JLabel currentPlayerlbl=new JLabel();
	//bottom-most panel for tools display
	JPanel MTPanel;
	JButton endTurnBtn;
	JButton[] MTSet;
	JLabel[] MTLabel;
	//action panel with 3 panels embedded
	CardLayout cl_ActionPanel;
	JPanel ActionPanel;
	
	JProgressBar[] progressBar;
	JProgressBar totalProgressBar=new JProgressBar();
	
	BoardWindow BoardPanel;
	CardWindow EventPanel;
	CardWindow SAPanel;
	CardWindow UseMTPanel;
	CardWindow EventPanel2;
	CardWindow drawMTPanel;
	
	
	public RoundWindow(final CardLayout cl,BoardEngine BE) 
	{
		//init local var
		this.boardEngine=BE;
		this.cl=cl;
		this.currentPlayer=0;	
		
		this.loadIcons();
		initLayout();
		initLocalPanel();
		
	}	
	
	private void loadIcons()
	{
		this.playerIcons=new Icon[6];
		for(int i=1;i<7;i++)
			this.playerIcons[i-1]=new ImageIcon("images\\player"+i+".gif");
		
	}
	
	private void initLayout()
	{
		setBounds(0,0,1024,768);
		setLayout(null);	
		
	}
	private void initLocalPanel()
	{
		setActionPanel();
		setMTPanel();
		setStatusPanel();
		StatusPanel.setBounds(0, 0, 224, 568);
		ActionPanel.setBounds(224, 0, 800, 568);
		MTPanel.setBounds(0,568,1024,200);
		add(StatusPanel);
		add(ActionPanel);
		add(MTPanel);
	}


	/*--- status panel ---*/
	public void setStatusPanel()
	{
		StatusPanel = new JPanel();
		StatusPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		StatusPanel.setBounds(0, 0, 224, 568);
		StatusPanel.setLayout(null);
		
		setSP_Player();
		setProgessBar();
		
		for(int i=0;i<6;i++)
		{
			this.StatusPanel.add(this.SP_Player[i]);
			this.StatusPanel.add(this.progressBar[i]);
		}
		//this.BoardPanel.add(this.currentPlayerlbl);
		this.StatusPanel.add(this.totalSElbl);
		this.StatusPanel.add(this.totalProgressBar);
	}
	
	private void setSP_Player()
	{	
		
		
		int y=50;
		this.SP_Player=new JLabel[6];
		this.progressBar=new JProgressBar[6];
		for(int i=1;i<7;i++,y+=60)
		{
			SP_Player[i-1]=new JLabel();
			SP_Player[i-1].setText("Player "+i);
			SP_Player[i-1].setIcon(this.playerIcons[i-1]);
			SP_Player[i-1].setFont(new Font("Arial", Font.PLAIN, 20));
			SP_Player[i-1].setVisible(false);
			progressBar[i-1]=new JProgressBar();
			progressBar[i-1]=new JProgressBar();
			progressBar[i-1].setStringPainted(true);
			progressBar[i-1].setValue(0);
			progressBar[i-1].setString(Integer.toString(progressBar[i-1].getValue()));
			progressBar[i-1].setVisible(false);
			SP_Player[i-1].setBounds(20, y, 105, 20);
			progressBar[i-1].setBounds(20, y+25, 105, 20);
		}
	}
	
	private void setProgessBar()
	{
		this.totalSElbl.setFont(new Font("Arial", Font.PLAIN, 20));
		this.totalSElbl.setVisible(false);	
		this.totalSElbl.setBounds(20, 435, 105, 20);
		this.totalProgressBar.setStringPainted(true);
		this.totalProgressBar.setValue(0);
		this.totalProgressBar.setString(Integer.toString(totalProgressBar.getValue()));
		this.totalProgressBar.setVisible(false);
		this.totalProgressBar.setBounds(20, 455, 105, 20);
	}
	
	/*-- MTPanel --*/
	private void setMTPanel()
	{
		this.MTPanel = new JPanel();
		this.MTPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		this.MTPanel.setLayout(null);
		this.initMTSet();
		this.initEndTurnBtn();
		
		for(int i=0;i<7;i++)
			this.MTPanel.add(this.MTSet[i]);
	
		this.MTPanel.add(this.endTurnBtn);
	}
	
	private void initMTSet()
	{
		this.MTSet=new JButton[7];
		this.MTLabel=new JLabel[7];
		int x=20;
		for(int i=0;i<7;i++,x+=105)
		{
			this.MTSet[i]=new JButton();
			this.MTSet[i].setVisible(false);
			this.MTLabel[i]=new JLabel();
			this.MTSet[i].addActionListener(this);
			this.MTSet[i].setBounds(x,20,80,120);
			this.MTSet[i].add(this.MTLabel[i]);
			this.MTSet[i].setLayout(null);
		}
	}
	
	private void initEndTurnBtn()
	{
		this.endTurnBtn= new JButton("End Turn");
		this.enableEndTurn(false);
		this.endTurnBtn.addActionListener(this);
		this.endTurnBtn.setBounds(860,20,100,33);	
	}
	
	/*-- action panel --*/
	private void setActionPanel()
	{
		ActionPanel = new JPanel();
		ActionPanel.setBorder(new LineBorder(new Color(0, 0, 0)));
		ActionPanel.setBounds(224, 0, 800, 568);
		ActionPanel.setLayout(new CardLayout(0, 0));
		cl_ActionPanel=(CardLayout) ActionPanel.getLayout();
		
		this.playerNo=this.boardEngine.getPlayerNo();
		this.boardEngine.initCardController(this);
		addBoardPanel();
		addUseMTPanel();
		addEventPanel();
		addSAPanel();
	}
	private void addUseMTPanel()
	{
		UseMTPanel=new UseMTWindow(this.cl,this, this.boardEngine.getUseMTController());
		ActionPanel.add(UseMTPanel);
		cl_ActionPanel.addLayoutComponent(UseMTPanel, "UseMTPanel");
	}
	private void addEventPanel()
	{
		EventPanel=new EventCardWindow(this.cl,this, this.boardEngine.getEventCardController());
		ActionPanel.add(EventPanel);
		cl_ActionPanel.addLayoutComponent(EventPanel, "EventPanel");
	}
	private void addSAPanel()
	{
		SAPanel=new SACardWindow(this.cl,this, this.boardEngine.getSACardController());
		ActionPanel.add(SAPanel);
		cl_ActionPanel.addLayoutComponent(SAPanel, "SAPanel");
	}
	
	private void addBoardPanel()
	{
		
		BoardPanel = new BoardWindow(this.playerIcons,this.playerNo,this.boardEngine);
		this.BoardPanel.initBoxType("Event", this.boardEngine.getBoxNo("Event"));
		this.BoardPanel.initBoxType("SA", this.boardEngine.getBoxNo("SA"));

		ActionPanel.add(BoardPanel);
		cl_ActionPanel.addLayoutComponent(BoardPanel, "BoardPanel");
		BoardPanel.setLayout(null);
		BoardPanel.setBorder(null);
		BoardPanel.setBounds(224, 0, 800, 568);
		
		printBoardPanel();
	}
	

	public int movePlayer(int currentPlayer, int step)
	{
		//currentPlayerlbl.setText("Current Player: "+Integer.toString(this.currentPlayer+1));
		//currentPlayerlbl.setFont(new Font("Arial", Font.PLAIN, 20));
		//currentPlayerlbl.setBounds(280, 200, 150, 20);
		
		for(int i=0;i<step;i++)
		{
			playerPos[currentPlayer]+=1;
			if(playerPos[currentPlayer]>=28)
			{
				this.boardEngine.drawPlayerMT(currentPlayer);
				playerPos[currentPlayer]-=28;
			}
			System.out.println("Current Player: "+(currentPlayer+1)+" "+"playerPos"+playerPos[currentPlayer]);
			this.printBoardPanel();
			this.printMTPanel();
		}
		return playerPos[currentPlayer];
	}
	public void printStatusPanel()
	{
		for(int i=0;i<this.playerNo;i++) 
		{
			SP_Player[i].setVisible(true);
			progressBar[i].setVisible(true);
		}
	}
	public void updateStatus()
	{
		
		
		int totalSE=0;
		for(int i=0;i<this.playerNo;i++) 
		{
			int printSE=this.boardEngine.printSE()[i];
			if(printSE>this.boardEngine.getWinCon())
				printSE=this.boardEngine.getWinCon();
			
			progressBar[i].setValue(printSE*100/this.boardEngine.getWinCon());
			progressBar[i].setString(Integer.toString(printSE)+"/"+this.boardEngine.getWinCon());
			
			
			totalSE+=this.boardEngine.printSE()[i];
		}
		
		if(this.boardEngine.getWinMeth()==1)
		{
			if(totalSE>this.boardEngine.getWinCon())
				totalSE=this.boardEngine.getWinCon();
			totalSElbl.setVisible(true);
			totalProgressBar.setVisible(true);
			totalProgressBar.setValue(totalSE*100/this.boardEngine.getWinCon());
			totalProgressBar.setString(Integer.toString(totalSE)+"/"+this.boardEngine.getWinCon());
		}
		this.printStatusPanel();
	}
	
	public void printBoardPanel()
	{
		System.out.println("Round Window "+(playerNo+1));
		this.BoardPanel.updatePlayerPos(this.playerPos);
		this.updateStatus();
		this.boardEngine.setAtPanel("Main");
		cl_ActionPanel.show(ActionPanel, "BoardPanel");
	}
	
	//update MT Panel once the player's round starts
	public void printMTPanel()
	{
		for(int i=0;i<7;i++)
			this.MTSet[i].setVisible(false);
		String[] currentMT=this.boardEngine.getCurrentPlayerMT();
		System.out.println("At RoundWIndow- currentMT length: "+currentMT.length);
		for(int i=0;i<currentMT.length;i++)
		{
			
			if(currentMT[i]=="Forward")
			{
				MTSet[i].setIcon(new ImageIcon("images\\MTs_Flight.gif"));
				MTSet[i].setEnabled(true);
			}
			else if(currentMT[i]=="Skip")
			{	
				MTSet[i].setIcon(new ImageIcon("images\\MTs_SlingShot.gif"));
				MTSet[i].setEnabled(false);
			}
			else if(currentMT[i]=="Bonus")
			{
				MTSet[i].setIcon(new ImageIcon("images\\MTs_Crown.gif"));
				MTSet[i].setEnabled(true);
			}
			this.MTSet[i].setText(currentMT[i]);
			
			this.MTSet[i].setVisible(true);
		}
	}
	public void printEventPanel()
	{
		this.boardEngine.setAtPanel("Event");
		cl_ActionPanel.show(ActionPanel, "EventPanel");
		EventPanel.removeAll();
	}

	public void printSAPanel()
	{
		this.boardEngine.setAtPanel("SA");
		cl_ActionPanel.show(ActionPanel, "SAPanel");
		SAPanel.removeAll();
	}
	
	/*--pending for refactoring --
	public void printSAPanelforMT()
	{
		cl_ActionPanel.show(ActionPanel, "SAPanel");
		//SAPanel.removeAll();
	}
	/*--pending for refactoring --
	public void printEventPanelforMT()
	{
		
		cl_ActionPanel.show(ActionPanel, "EventPanel");
		//EventPanel.removeAll();
	}
	*/
	/* display the UseMTPanel */
	
	public void printCardPanel(String type)//( String[] type, int MTNo)
	{
		this.endTurnBtn.setEnabled(false);	
		if(type=="UseMT")
			cl_ActionPanel.show(ActionPanel, "UseMTPanel");
		else if(type=="Event")
			cl_ActionPanel.show(ActionPanel, "EventPanel");
		else if(type=="SA")
			cl_ActionPanel.show(ActionPanel, "SAPanel");
	}
	
	public void enableEndTurn(boolean enable)
	{
		if(enable)
			this.btnState=true;
		
		this.endTurnBtn.setEnabled(enable);
	}
	
	public void finishRollDice()
	{
		this.btnState=true;
		
	}
	public void disableEndBtn()
	{
		this.endTurnBtn.setEnabled(false);
	}
	public void checkEndBtnState()
	{
		this.enableEndTurn(this.btnState);
	}
	
	public void setPlayerNo(int playerNo)
	{
		this.playerNo=playerNo;
	}
	public void setPlayerPos()
	{
		this.playerPos=new int[this.playerNo];
		for(int i=0;i<this.playerNo;i++)
			this.playerPos[i]=0;
	}
	//called by UseMTWindow to make a particular card invisible 
	public void setMTVisible(int btnIndex, boolean visible)
	{
		this.MTSet[btnIndex].setVisible(visible);	
	}
	
	public void setMTEnabled(int btnIndex, boolean enable)
	{
		this.MTSet[btnIndex].setEnabled(enable);
	}
	@Override
	public void actionPerformed(ActionEvent e) 
	{
		for(int i=1;i<8;i++)
		{
			if(e.getSource()==this.MTSet[i-1])
			{
				useMT(i);
				printCardPanel("UseMT");
				
				//movePlayer(this.boardEngine.getCurrentPlayer(), i);
			}
		}
		if(e.getSource() == endTurnBtn)
		{
			if(this.boardEngine.fulfillWinCon())
			{
				JOptionPane.showMessageDialog(null,this.boardEngine.getWinningMsg());
			}
			else	
			{
				startNextTurn();
				printMTPanel();
				this.boardEngine.setAtPanel("Main");
			}
		}
	}
	
	public void useMT(int cardNo)
	{
		((UseMTWindow) UseMTPanel).setLabelValue("MT"+cardNo);
		((UseMTWindow) UseMTPanel).setBigMTValue(this.MTSet[cardNo-1].getLabel());
		((UseMTWindow) UseMTPanel).setPlayerInfo(this.boardEngine.getCurrentPlayer(), cardNo-1);
		((UseMTWindow) UseMTPanel).setDisplay();
		this.enableEndTurn(false);	
	}
	
	
	public void startNextTurn()
	{
		this.BoardPanel.resetBtnState();
		this.BoardPanel.enableRollBtn(true);
		boardEngine.switchNextPlayer();
		this.btnState=false;
		this.enableEndTurn(false);
		this.currentPlayer=boardEngine.getCurrentPlayer();
		System.out.println("Round window current player: "+(this.currentPlayer+1));
	}
	
	public BoardWindow getBoardWindow() 
	{
		return this.BoardPanel;
	}

	public int getPlayerPos(int i) 
	{
		return this.playerPos[i];
	}

}
	
